package com.monte.persistence;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.monte.persistence.command.BatchDeleteEntityCommand;
import com.monte.persistence.command.BatchInsertCommand;
import com.monte.persistence.command.BatchUpdateEntityCommand;
import com.monte.persistence.command.DeleteEntityCommand;
import com.monte.persistence.command.InsertEntityCommand;
import com.monte.persistence.command.UpdateEntityCommand;

/**
 * @描述:持久化管理类
 * @author FengMy
 * @since 2013-3-4
 */
@Component(value="persistenceManager")
public class PersistenceManagerImpl extends SqlSessionDaoSupport implements PersistenceManager{
	@Autowired
	protected SqlSessionFactory sqlSessionFactory;
	@Autowired
	protected JdbcTemplate jdbcTemplate;
	@PersistenceContext
	private EntityManager entityManager;
	
	@Override
	public EntityManager getEntityManager() {
		return entityManager;
	}
	
	/**
	 * 新增实体
	 * @param entity
	 * @return
	 */
	@Override
	public <T> T addEntity(T entity){
		executeCommand(new InsertEntityCommand(entity));
		return entity;
	}
	
	/**
	 * 批量持久化新实体
	 * @param entities
	 * @return
	 */
	@Override
	public <T> int addBatch(List<T> entities) {
		return (Integer) executeCommand(new BatchInsertCommand<T>(entities));
	}
	
	/**
	 * 更新实体
	 * @param entity
	 */
	@Override
	public <T> T updateEntity(T entity){
		executeCommand(new UpdateEntityCommand<T>(entity));
		return entity;
	}
	
	/**
	 * 批量更新实体
	 * @param entities
	 */
	@Override
	public <T> int updateBatch(List<T> entities){
		return (Integer) executeCommand(new BatchUpdateEntityCommand<T>(entities));
	}
	
	/**
	 * 删除实体
	 * @param entity
	 */
	@Override
	public <T> int deleteEntity(T entity){
		return (Integer) executeCommand(new DeleteEntityCommand<T>(entity));
	}
	
	/**
	 * 删除实体
	 * @param entity
	 */
	@Override
	public <T> int deleteBatch(List<T> entities){
		return (Integer) executeCommand(new BatchDeleteEntityCommand<T>(entities));
	}
	
	/**
	 * 根据id获取实体
	 * @param <T>
	 * @param id
	 * @return
	 */
	@Override
	public <T> T getEntityById(Class<T> type,Object id){
		return entityManager.find(type, id);
	}
	
	/**
	 * 创建并返回当前持久化上下文
	 * @return
	 */
	private CommandContext getCurrentCommandContext(){
		CommandContext ctx = new CommandContext();
		ctx.setEntityManager(entityManager);
		ctx.setJdbcTemplate(jdbcTemplate);
		ctx.setSqlSession(getSqlSession());
		ctx.setSqlSessionFactory(sqlSessionFactory);
		return ctx;
	}
	
	/**
	 * 执行命令
	 * @param command
	 * @return
	 */
	@Override
	public Object executeCommand(PersistenceCommand command) {
		if(command != null){
			return command.execute(getCurrentCommandContext());
		}
		return null;
	}
}
